var chek_login_url = web_url + '/owner/user/check-login'+'.html';
var do_login_url = web_url + '/owner/user/do-login'+'.html';
var coll_ulr =  web_url + '/home/head/collect_head'+'.html';
var no_coll_url =  web_url + '/home/head/no_collect_head'+'.html';
var reload,
	ajax,
	jump_url ,
	coll_id;

$('.modal-close').click(function(){
	$('.login-show').hide();
	//把所有信息都清空
	$('.login-show input').removeClass('border-orange').val('');
	$('.login-show span.warning').hide();
})

$('input').focus(function(evnet){
    $('.login_info').hide();
})

//再模态框中点击登录登录(ajax)
$('.login_btn').click(function(event){
	var e = window.event || event,
       errNum = 0;

    if(e.preventDefault){
        e.preventDefault();
    }else{
        window.event.returnValue = false; 
    }

    //触发验证
    $('input').trigger('blur');
	errNum = $('span.warning:visible').length;

	//登录
	if(errNum == 0){
		$.ajax({
            async : true,
            url   : do_login_url,
            type  : 'post',
            data  : {'mobile':$('input[name="mobile"]').val(),'password': $('input[name="password"]').val()},
            dataType :'json',
            success:function(data){
                if(data.code == 400){
                    $('.login_info').show().find('span.warning').show();

                }else{
                	
                	if(reload == 1){
                		location.reload(); 
                	}else if(ajax == 1){

                		
                	}else{
                		//直接跳转到该链接
                		location.href = jump_url;
                	}
     				                
                }
            }
        })
	}
})

/**
 * [coll_css 给按钮添加收藏成功和未收藏的样式]
 * @param  {[type]} $col_btn [description]
 * @param  {[type]} $type    [1-收藏成功的样式,0-取消收藏的样式]
 * @return {[type]}          [description]
 */
function coll_css($col_btn,$type)
{
	if($type == 1){
		console.log('已收藏');
		
		//收藏成功的样式
		$col_btn.removeClass('back87C743')
				.removeClass('collect')
				.addClass('no_collect')
				.addClass('back999')
				.attr('href',no_coll_url)
				.html('已收藏');
	}else{
		//取消收藏的样式
		$col_btn.removeClass('back999')
				.removeClass('no_collect')
				.addClass('collect')
				.addClass('back87C743')
				.attr('href',coll_ulr)
				.html('收藏');
	}	
}

$('body').delegate('.collect','click',function(event){

	var e = window.event || event,
		that = $(this);
		reload = $(this).data('reload');
		ajax   = $(this).data('ajax');
		jump_url = $(this).attr('href');
		coll_id = $(this).data('coll_id');
	
	if(e.preventDefault){
		e.preventDefault();
	}else{
		e.retrunValue = false;
	}
	
	$.ajax(
		{
			type 	: 'POST',
			url  	: chek_login_url,
			async	: true,
			dataType:'json',
			success :function(data){
				if(data.code == 200){
					that.removeClass('collect').attr('href','javascript::void()');
					
					//成功
					if(ajax == 1){
                		// alert(jump_url);

             			//开始ajax收藏
                		$.ajax({
                			async : true,
				            url   : jump_url,
				            type  : 'post',
				            data  : {
				            	'coll_id' : coll_id
				            },
				            dataType :'json',
				            success:function(data){
				            	if(data.code == 200){

				            		//收藏成功
				            		if(data.collected == 1){

				            			if( identity && identity==3){
				            				if($('.owner_coll_num_p').length>0){
				            					$('.owner_coll_num_p').html(parseInt($('.owner_coll_num_p').html())+1);
				            				}
				            			}
				            			coll_css(that,1);
				            		}else if(data.collected == 0){
				            			coll_css(that,0);
				            		}
				            		
				            		
				            	}else{
				            		//收藏失败
				            		coll_css(that,0);
				            		
				            	}
				            }
                		})
                	}else{
                		//直接跳转到该链接
                		location.href = jump_url;
                	}
				}else{

					//失败显示登录模态框
					$('.login-show').show();
				}
			}
		}
	)
}).delegate('.no_collect','click',function(event){
	var e = window.event || event,
		that = $(this);
		reload = $(this).data('reload');
		ajax   = $(this).data('ajax');
		jump_url = $(this).attr('href');
		coll_id = $(this).data('coll_id');
	if(e.preventDefault){
		e.preventDefault();
	}else{
		e.retrunValue = false;
	}

	$.ajax({
    			async : true,
	            url   : jump_url,
	            type  : 'post',
	            data  : {
	            	'coll_id' : coll_id
	            },
	            dataType :'json',
	            success:function(data){
	            	if(data.code == 200){

	            		//收藏成功
	            		if(data.collected == 1){
	            			coll_css(that,1);
	            		}else if(data.collected == 0){
	            			if(identity && identity==3){
				            				if($('.owner_coll_num_p').length>0){
				            					$('.owner_coll_num_p').html(parseInt($('.owner_coll_num_p').html())-1);
				            				}
				            			}
	            			coll_css(that,0);
	            		}
	            		
	            		
	            	}else{
	            		//收藏失败
	            		coll_css(that,0);
	            		
	            	}
	            }
    })
})

